import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:search_people/page/dynamic/subs/dynamic_person/widgets/section.dart';

import '../../../../utils/component/list/section.dart';
import '../../../../utils/date_format/date_util.dart';
import '../../../../utils/util.dart';
import '../../../home/main/views/dynamic_cell.dart';
import '../../../home/subs/person_detail/models/person_dynamic_model.dart';
import 'index.dart';

class DynamicPersonController extends GetxController {
  DynamicPersonController();

  final state = DynamicPersonState();

  // tap
  void actionFilterDate() {
    showModalBottomSheet(
      // 底部颜色为透明，用Container修饰顶部圆角
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      context: Get.context!,
      builder: (BuildContext content) {
        return SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.only(top: 20.sp),
            clipBehavior: Clip.antiAlias,
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(20.0),
                topRight: Radius.circular(20.0),
              ),
            ),
            child: Column(
              children: [
                BrnCalendarView(
                  showControllerBar: true,
                  displayMode: DisplayMode.month,
                  selectMode: SelectMode.range,
                  rangeDateChange: (DateTimeRange range) {
                    state.begin = range.start;
                    state.end = range.end;
                    var beginTimeStr = DateFormat.format(
                        range.start, DateFormatType.ymd,
                        dateSeprator: '.');

                    var endTimeStr = DateFormat.format(
                        range.end, DateFormatType.ymd,
                        dateSeprator: '.');
                    state.date = '$beginTimeStr~$endTimeStr';
                    update();
                  },
                ),
                SafeArea(
                  child: Container(
                    margin: EdgeInsets.only(top: 10.sp, bottom: 5.sp),
                    width: Get.width / 5 * 4,
                    child: ElevatedButton(
                      onPressed: () {
                        Get.back();
                      },
                      style: ElevatedButton.styleFrom(
                        primary: Color(0xff58C165),
                        shape: StadiumBorder(),
                      ),
                      child: Text("确定"),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  //获取工作动态
  void getPersonDynamic() async {
    var resultData = await Api.getPersonDynamics(state.leaderId!);
    List data = resultData.data['records'];
    state.dynamics = data.map((e) => PersonDynamicModel.fromJson(e)).toList();

    state.list = [
      SectionModel(
          index: 0,
          sectionHeader: PersonDynamicSectionWidget(),
          itemBuilder: (ctx, index) {
            var md = state.dynamics![index];
            return DynamicCell(
              md: md,
            );
          },
          items: state.dynamics ?? [])
    ];
    update();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.leaderId = '4cc35aa0-ab19-49fc-8f0a-76f7f0491592';
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    getPersonDynamic();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
